<?php
defined('_JEXEC') or die('Restricted access');
$app = JFactory::getApplication();
$input = $app->input;
$action = $input->get('action');

$itemid = $input->get('Itemid');
$items = $this->get('Items');
$this->pagination = $this->get('pagination');
$view = $input->get('view');
$task = $input->get('task');
$AppList =  $this->get('App');
$param_type = array('-1'=>'None', '1'=>'Counting', '2'=>'Computing');
?>

<?php if ($action == 'create'): ?>
<script type="text/javascript">
    $(function(){
        $("#comp_param_type").change(function(){
            if($(this).val()==-1){
                $("#comp_param_name").fadeOut();
            }else{
                $("#comp_param_name").fadeIn();
            }
        })
    })
</script>
<div class="res-title-rg">
    <span class="fl pos-icon text-c"><b class="glyphicon glyphicon-plus"></b></span>
    <div class="title-info">
        <h2 class="fontsize20">Create an event</h2>
        <p><a href="#">Home</a>&nbsp;/&nbsp;Setup&nbsp;/&nbsp;Create an event</p>
    </div>
</div>
<div class="ws-content pdall15">
	<div style="width:600px;margin:auto">
		<div style="padding:32px 0 4px 0;text-align:right;font-size:18px;border-bottom:solid 2px #000">Event Form</div>
		<div style="height:32px"></div>
	    <form method="post" class="form-horizontal J_ajaxFsorm" action="/index.php?option=com_console&Itemid=127&task=<?php echo $task ; ?>&action=store">
	    	<div class="form-group">
	    		<label for="app" class="col-sm-2 control-label">Application</label>
	    		<div class="col-sm-10">
		    		<select class="form-control" id="app" name="app_id">
	                    <?php foreach ($AppList as $key => $value): ?>
	                        <option value="<?php echo $value->id ?>"><?php echo $value->name ?></option>
	                    <?php endforeach ?>
	                </select>
	            </div>
	    	</div>
	        <div class="form-group">
	            <label for="id" class="col-sm-2 control-label">Id</label>
	            <div class="col-sm-10">
	            	<input type="text" class="form-control" id="id" name="event_id" placeholder="Event id to identify this event">
	            </div>
	        </div>
	        <div class="form-group">
	            <label for="name" class="col-sm-2 control-label">Name</label>
	            <div class="col-sm-10">
	            	<input type="text" class="form-control" id="name" name="event_name" placeholder="A name to make event id human readable">
	            </div>
	        </div>
	        <div class="form-group">
	            <label for="type" class="col-sm-2 control-label">Type</label>
	            <div class="col-sm-10">
	            	<select class="form-control" name="comp_param_type" id="type">
              		<?php foreach ($param_type as $key => $value): ?>
                    	<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
                	<?php endforeach ?>  
            		</select>
	            </div>
	        </div>
	        <div class="form-group">
	            <label for="param_name" class="col-sm-2 control-label">Parameter name</label>
	            <div class="col-sm-10">
	            	<input type="text" class="form-control" id="param_name" name="comp_param_name" placeholder="Parameter name of Counting or Computing event">
	            </div>
	        </div>
	        <div class="form-group">
	        	<div class="col-sm-offset-2 col-sm-10">
	        		<button type="submit" class="btn btn-default J_ajax_submit_btn">Create</button>
	        	</div>
	        </div>
	    </form>
	</div>
</div>
<?php elseif ($action == 'edit'): ?>
    <?php
    $model = $this->getModel();
    $Item = $model->getItem( (int) $input->get('id'));
    ?>
    <script type="text/javascript">
        $(function(){
            $("#comp_param_type").change(function(){
                if($(this).val()==-1){
                    $("#comp_param_name").fadeOut();
                }else{
                    $("#comp_param_name").fadeIn();
                }
            })
        })
    </script>
<div class="res-title-rg">
    <span class="fl pos-icon text-c"><i class="setup"></i></span>
    <div class="title-info">
        <h2 class="fontsize20">Edit Events</h2>
        <p><a href="#">Home</a>&nbsp;/&nbsp;Setup&nbsp;/&nbsp;Edit Events</p>
    </div>
</div>
<div class="ws-content pdall15">
	<div style="width:600px;margin:auto">
		<div style="padding:32px 0 4px 0;text-align:right;font-size:18px;border-bottom:solid 2px #000">Event Form</div>
		<div style="height:32px"></div>
	    <form method="post" class="form-horizontal J_ajaxFsorm" action="/index.php?option=com_console&Itemid=127&task=<?php echo $task ; ?>&action=update">
	    	<input type="hidden" name="id" value="<?php echo $Item->id;?>">
	    	<div class="form-group">
	    		<label for="app" class="col-sm-2 control-label">Application</label>
	    		<div class="col-sm-10">
	    			<input type="hidden" name="app_id" value="<?php echo $Item->app_id ?>" />
		    		<select class="form-control" id="app" disabled="disabled">
	                    <?php foreach ($AppList as $key => $value): ?>
                        <option value="<?php echo $value->id ?>" <?php if($Item->app_id == $value->id):?>selected<?php endif;?>><?php echo $value->name ?></option>
                    <?php endforeach ?>
	                </select>
	            </div>
	    	</div>
	        <div class="form-group">
	            <label for="id" class="col-sm-2 control-label">Id</label>
	            <div class="col-sm-10">
	            	<input type="text" class="form-control" id="id" name="event_id" value="<?php echo htmlspecialchars($Item->event_id);?>" placeholder="Event id to identify this event">
	            </div>
	        </div>
	        <div class="form-group">
	            <label for="name" class="col-sm-2 control-label">Name</label>
	            <div class="col-sm-10">
	            	<input type="text" class="form-control" id="name" name="event_name" value="<?php echo htmlspecialchars($Item->event_name);?>" placeholder="A name to make event id human readable">
	            </div>
	        </div>
	        <div class="form-group">
	            <label for="type" class="col-sm-2 control-label">Type</label>
	            <div class="col-sm-10">
	            	<select class="form-control" name="comp_param_type" id="type">
              		<?php foreach ($param_type as $key => $value): ?>
                    	<option value="<?php echo $key; ?>" <?php if($Item->comp_param_type == $key):?>selected<?php endif;?>><?php echo $value; ?></option>
                	<?php endforeach ?>  
            		</select>
	            </div>
	        </div>
	        <div class="form-group">
	            <label for="param_name" class="col-sm-2 control-label">Parameter name</label>
	            <div class="col-sm-10">
	            	<input type="text" class="form-control" id="param_name" name="comp_param_name" value="<?php echo $Item->comp_param_name?>" placeholder="Parameter name of Counting or Computing event">
	            </div>
	        </div>
	        <div class="form-group">
	        	<div class="col-sm-offset-2 col-sm-10">
	        		<button type="submit" class="btn btn-default J_ajax_submit_btn">Update</button>&nbsp;&nbsp;&nbsp;&nbsp;
	        		<a class="btn btn-danger  layer_confirm" title="are you sure?" href="/index.php?option=com_console&Itemid=127&task=<?php echo $task ; ?>&action=delete&id=<?php echo $Item->id?>">Delete</a>
	        	</div>
	        </div>
	    </form>
	</div>
</div>
<?php else: //列表?>
<div class="res-title-rg">
    <span class="fl pos-icon text-c"><i class="setup"></i></span>
    <div class="title-info">
        <h2 class="fontsize20">Events</h2>
        <p><a href="#">Home</a>&nbsp;/&nbsp;Setup&nbsp;/&nbsp;Events</p>
    </div>
</div>
<div class="ws-content pdall15">
	<div class="camtab">
		<a href="/index.php?option=com_console&view=<?php echo $view; ?>&Itemid=127&task=<?php echo $task ?>&action=create" class="fr btn-popup fontsize16">+&nbsp;Create an event</a>
	</div>
	<form action="<?php echo htmlspecialchars(JUri::getInstance()->toString()); ?>" method="post" name="adminForm" id="adminForm">
		<div style="padding:8px">
 			<table class="table table-hover">
             	<thead>
             		<tr>
             			<th>Id</th> 
             			<th>Name</th>  
             			<th>Type</th> 
             			<th>Parameter Name</th>
             			<th>Application</th>
             		</tr>
             	</thead>
             	<tbody>
             	<?php if ($items): ?>
             	<?php foreach ($items as $key => $item): ?>
              		<tr>
             			<td><?php echo $item->event_id ?></td>
             			<td>
             				<a href="/index.php?option=com_console&view=<?php echo $view; ?>&Itemid=127&task=<?php echo $task ; ?>&action=edit&id=<?php echo $item->id?>"><?php echo $item->event_name; ?></a>
             			</td>
             			<td><?php echo $param_type[$item->comp_param_type] ; ?></td>
             			<td>
                 			<?php
                 				if ($item->comp_param_type != -1) {
                     				echo $item->comp_param_name;
                 				}
                 			?>
             			</td>
             			<td><?php echo $item->name ?></td>
           			</tr>
               <?php endforeach ?>
               <?php endif;?>
               </tbody>
         	</table>
			<div class="pagination">
				<?php echo $this->pagination->getPagesLinks(); ?>
			</div>
		</div>
    </form>
</div>

<?php endif;?>
